<template>
  <div class="return_ad">
    <div class="warn-box">
      <svg-icon
        style="margin-right: 17px"
        icon-class="warn"
        :size="20"
      />
      <p>归还地址为用户申请归还商品时，订单页面展示的物流归还地址。</p>
    </div>
    <div class="flex align-center table-top-btns">
      <h5 class="flex align-center">
        <svg-icon
          style="margin-right:15px"
          icon-class="list"
          :size="15"
        />数据列表
      </h5>
      <el-button
        v-if="$hasMethod('#add')"
        size="small"
        class="add-btn"
        icon="el-icon-plus"
        @click="$router.push({name:'shopAddReturnAd'})"
      >添加归还地址</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="id"
        label="ID"
        width="70"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="realname"
        label="收货人姓名"
        align="center"
        width="110"
      >
      </el-table-column>
      <el-table-column
        prop="mobile"
        label="手机"
        width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="province"
        label="省份"
        width="80"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="city"
        label="城市"
        width="80"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="area"
        label="区/县"
        align="center"
      >
      </el-table-column>
      <el-table-column
        label="地址"
        align="center"
      >
        <template slot-scope="scope">
          <p>{{scope.row.address}}
            <el-tag
              v-if="scope.row.isdefault==1"
              class="tagClass"
              size="small"
              type="danger"
            >默认地址</el-tag>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        prop="addtime"
        label="创建时间"
        align="center"
        width="100"
      >
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        width="80"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag
            class="tagClass"
            size="small"
            :class="{'tag_success':scope.row.status==2,'tag_danger':scope.row.status==1,'pointer':$hasMethod('#enable')}"
            :type="scope.row.status == 2 ? 'success' : 'danger'"
            @click="changeStatus(scope.row.id,scope.row.status)"
          >{{scope.row.status == 1 ? "禁用" : "启用"}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="280"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            v-if="$hasMethod('#edit')"
            @click="editItem(scope.row.id)"
          >编辑</el-button>
          <el-button
            size="small"
            type="danger"
            v-if="$hasMethod('#delete')"
            @click="deleteItem(scope.row.id)"
          >删除</el-button>
          <el-button
            size="small"
            type="warning"
            v-if="$hasMethod('#set')&&scope.row.isdefault!=1"
            @click="setItem(scope.row.id)"
          >设为默认地址</el-button>
        </template>
      </el-table-column>
    </el-table>
    <custom-page
      ref="customPage"
      :total="total"
      @getList="getList"
    ></custom-page>
  </div>
</template>

<script>
import customPage from "@/components/customPage";
import { returnAddressList, delReturnAddress, addOrEditReturnAddress } from "@/api/shop";
export default {
  components: { customPage },
  data() {
    return {
      tableData: [],
      total: 0
    }
  },
  methods: {
    changeStatus(id, status) {
      if (!this.$hasMethod("#enable")) {
        return;
      }
      let tip = "", title = "";
      if (status == 2) {
        tip = "您确定要禁用吗？";
        title = "禁用";
      } else {
        tip = "您确定要启用吗？";
        title = "启用";
      }
      this.$confirm(tip, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        addOrEditReturnAddress({
          id,
          status: status == 2 ? 1 : 2
        }).then(res => {
          this.getList();
        })
      }).catch(() => { });
    },
    setItem(id) {
      addOrEditReturnAddress({ id, isdefault: 1 }).then(res => {
        this.getList();
      })
    },
    deleteItem(id) {
      this.$confirm('您确定要删除吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        delReturnAddress(id).then(res => {
          this.getList();
        });
      }).catch(() => { });
    },
    editItem(id) {
      this.$router.push({ name: 'shopAddReturnAd', params: { editId: id } });
    },
    getList() {
      const { page, pageSize } = this.$refs.customPage;
      returnAddressList({
        page,
        pageSize
      }).then(res => {
        this.tableData = res.data || [];
        this.total = res.total || 0;
      })
    }
  },
  mounted() {
    this.getList();
  }
}
</script>

<style lang="scss" scoped>
</style>
